package com.sun.mybatisplus.rabbitmq.controller;

import com.sun.mybatisplus.rabbitmq.service.OrderService;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auth: syh
 * @Date: 2021/10/22 14:33
 * @Email: 1014170249@qq.com
 * @Desc: 不在能知，而在能行
 */
@RestController
public class RabbitMqController {

    @Autowired
    private AmqpAdmin amqpAdmin;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private OrderService orderService;


    /**
     * 创建队列
     * @return
     */
    @RequestMapping("/create")
    public String createQueues(){
        //创建交换机
        amqpAdmin.declareExchange(new FanoutExchange("my_exchange"));
        //创建队列
        String myQueue = amqpAdmin.declareQueue(new Queue("my_queue", true));
        //创建路由key
        amqpAdmin.declareBinding(new Binding(myQueue,Binding.DestinationType.QUEUE,"my_exchange","my_key",null));
        rabbitTemplate.convertAndSend("my_exchange","my_key","我是新产生的消息");
        return "创建队列完成，并发送消息";
    }


    /**
     * 模拟用户发送订单
     */
    @RequestMapping("/order")
    public void makeOrder(){
        orderService.makeOrder("1","product_001",12);
    }


    /**
     * 模拟用户发送订单 Direct模式
     */
    @RequestMapping("/direct")
    public void makeOrderDirect(){
        orderService.makeOrderDirect("1","product_002",500);
    }

    /**
     * 模拟用户发送订单 Topic模式
     * 主题模式
     */
    @RequestMapping("/top")
    public void makeOrderTopic(){
        orderService.makeOrderTopic("1","product_003",666);
    }


    /**
     * 模拟用户发送订单 ttl  设置队列的过期时间
     * 主题模式
     */
    @RequestMapping("/ttl")
    public void makeOrderTTL(){
        for (int i = 0; i < 11; i++) {
            orderService.makeOrderTTL("1","product_004",9999);
        }
    }



    /**
     * 模拟用户发送订单 ttl  设置消息的过期时间
     * 主题模式
     */
    @RequestMapping("/mess")
    public void makeOrderTTLMessage(){
        orderService.makeOrderTTLMessage("1","product_005",55555);
    }
}
